<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/10/14 16:56
 */

namespace module\ai\library\channel\exception;

use Throwable;

class ChatException extends \RuntimeException
{
    protected array $data = [];

    public function __construct($message = "", $code = 2, Throwable $previous = null)
    {
        parent::__construct($message, $code, $previous);

        $this->data = [
            'index'         => 0,
            'content'       => $this->message,
            'errcode'       => $this->code,
            'finish_reason' => 'error',
            'status'        => 'finish',
        ];
    }

//    public function getCompletion(): ErrorCompletion
//    {
//        return new ErrorCompletion([
//            'message' => $this->getMessage(),
//            'code'    => $this->getCode(),
//        ]);
//    }

    public function setData(array $data): void
    {
        $this->data = $data;
    }

    public function data(): array
    {
        return $this->data;
    }

    public function see($json = true): bool|array|string
    {
        $result = [
            'index'         => 0,
            'content'       => $this->message,
            'errcode'       => $this->code,
            'finish_reason' => 'error',
            'status'        => 'finish',
        ];

        if (env('app_debug')) {
            $result['raw'] = $this->data;
        }

        return $json ? json_encode($result, JSON_UNESCAPED_UNICODE) : $result;
    }
}